package com.august.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class TestImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public void checkCompany(){
    //获取数据
//    List<CompanyVo> omsList = getOmsList();
//    //将要比对数据放入Map
//    Map<Long, CompanyVo> omsMap = new HashMap<Long, CompanyVo>();
//        for(CompanyVo vo:omsList){
//            omsMap.put(vo.getIdUuid(), vo);
//        }
//        //比较数据
//        compareCompany(omsMap);
    }

//    public void compareCompany(Map<Long, CompanyVo> omsMap){
//        int count = jdbcTemplate.queryForObject("select count(1) from oms_company", Integer.class);
//        int pages = count%2000 == 0 ?(count/2000):(count/2000 + 1);
//        ExecutorService executor = Executors.newFixedThreadPool(10);
//        for(int i = 0; i < pages; i++){
//            int startIndex = i*2000;
//
//            ProTask task = new ProTask(startIndex, omsMap);
//
//            FutureTask future = new FutureTask(task,null);
//
//            executor.execute(future);
//        }
//
//
//        executor.shutdown();
//        //判断是否比对完成
//        while(true){
//            if(executor.isTerminated()){
//                break;
//            }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }

    public List<CompanyVo> getOmsList(){
        List<CompanyVo> list = new ArrayList<>();

        int count = jdbcTemplate.queryForObject("select count(1) from TB_PUBHA", Integer.class);
        int pages = count%2000 == 0?(count/2000):(count/2000 + 1);
        ExecutorService executor = Executors.newFixedThreadPool(20);
         List<FutureTask<List<CompanyVo>>> futureList = new ArrayList<>();
        for(int i = 0; i < pages; i++){
            int start = i*2000 + 1;
            int end = (i+1)*2000;
            if(end > count) end = count;
            OmsTask task = new OmsTask(start, end);
            FutureTask<List<CompanyVo>> future = new FutureTask<>(task);
            executor.submit(future);
            futureList.add(future);
         }
        executor.shutdown();
        for(FutureTask<List<CompanyVo>> future:futureList){
            try {
                List<CompanyVo> listq = future.get();
                if(null != listq && !listq.isEmpty())
                    list.addAll(listq);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


}
